package com.sam;
import java.io.IOException;
import java.net.URL;
import javax.servlet.http.*;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.api.urlfetch.*;


public class HelloSDC extends HttpServlet {
	
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();

    URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService();

    //modified for secure data access
    URL dataURL = new URL("http://htpc/foo.php");
    HTTPRequest fetchreq = new HTTPRequest(dataURL);
    fetchreq.setHeader(new HTTPHeader("use_intranet","yes"));

    if (user != null) {
      resp.setContentType("text/plain");
      resp.getWriter().println("Hello, " + user.getNickname());
      resp.getWriter().println("Fetching your data...");

      HTTPResponse fetchresp = fetcher.fetch(fetchreq);

      resp.getWriter().println("Response Code: " + fetchresp.getResponseCode());
      resp.getWriter().print(new String(fetchresp.getContent()));
    } else {
      resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
    }
  }
}